<?php

/**
 * @version     $Id: possibleusers.php 3 2010-10-08 17:49:35Z obalyuk87@gmail.com $
 * @package     AdminSafe
 * @subpackage  Components
 * @copyright   Copyright (C) 2010 Oleksandr Balyuk (www.boolcast.com)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL v2
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
// No direct access
defined('_JEXEC') or die('Restricted access');
require_once JPATH_COMPONENT_ADMINISTRATOR . DS . 'classes' . DS . 'component' . DS . 'model.php';

/**
 * Description of accesslog
 *
 * @author Oleksandr Balyuk <obalyuk@boolcast.com>
 */
class AdminsafeModelPossibleusers extends AdminsafeModel {

    /**
     * Holds list of users by ip adddress
     * @var array
     */
    protected $_usersByIp = array();

    /**
     * Gets data
     *
     * @return array
     */
    public function getData() {

        if (!$this->_data) {
            $data = parent::getData();

            //  Load Users
            foreach ($data as $item) {
                if (!isset($item->users)) {
                    $item->users = $this->_getUsersByIp($item->ip_address);
                    $item->userLink  = $this->_convertUsersListToHTML($item->users);
                }
            }
        }

        return $this->_data;
    }


    /**
     * Loads users by IP Address
     *
     * @param string $ip
     * @return array
     */
    public function _getUsersByIp($ip) {
        //  Remember Index
        $idx = $ip;

        if (!isset($this->_usersByIp[$idx])) {

            //  Escape & Quote the IP Address
            $ip = $this->_db->Quote($ip);

            //  Build Query
            $query = 'SELECT u.id, u.name'
                    . ' FROM #__adminsafe_access_log AS al '
                    . ' JOIN #__users AS u ON u.id = al.user_id '
                    . " WHERE al.ip_address = $ip"
                    . ' GROUP BY u.id'
                    . ' ORDER BY u.name';

            //  Load list of users
            $this->_usersByIp[$idx] = & $this->_getList($query);
        }

        return $this->_usersByIp[$idx];
    }


    /**
     * Converts list of users to HTML
     *
     * @param array $users
     * @return string
     */
    protected function _convertUsersListToHTML($users) {

        //  Scan through all possible users for the ip
        $newUsers = array();
        if ($users) {
            foreach ($users as $user) {
                if ($user) {
                    $url = JRoute::_("index.php?option=com_users&view=user&task=edit&cid[]=$user->id");
                    $newUsers[] = JHTML::link($url, $user->name);
                }
            }
        }
        //  Assign userLink
        return ($newUsers) ? implode('<br />', $newUsers) : null;
    }

}